package com.haisen.controller;


import com.alibaba.fastjson.JSON;
import com.haisen.core.annotation.SystemLog;
import com.haisen.core.utils.PageResultSet;
import com.haisen.core.utils.Result;
import com.haisen.core.utils.SpringBeanUtil;
import com.haisen.dataobject.entity.BussItem;
import com.haisen.dataobject.entity.JobSchedule;
import com.haisen.dataobject.entity.JobVerifyCode;
import com.haisen.dataobject.query.BussItemQuery;
import com.haisen.dataobject.query.JobScheduleQuery;
import com.haisen.dataobject.query.JobVerifyCodeQuery;
import com.haisen.service.JobNoService;
import com.haisen.service.JobScheduleService;
import com.haisen.service.JobVerifyCodeService;
import com.haisen.service.impl.WebSocketService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@Controller
@RequestMapping("/jobsch")
@Slf4j
public class JobScheduleController {
    @Autowired
    private JobScheduleService jobScheduleService;

    @Autowired
    private JobNoService jobNoService;

    private static WebSocketService webSocketService;

    @GetMapping
    @RequiresPermissions("jobsch:view")
    public String page(Model model){
        setCommonDate(model);
        return "produce/jobsch";
    }

    @ResponseBody
    @RequiresPermissions("jobsch:view")
    @RequestMapping("/list")
    public PageResultSet<JobSchedule> list(JobScheduleQuery jobScheduleQuery) {
        //页面显示改为验证码推送记录
        return jobScheduleService.findByPage(jobScheduleQuery);
    }

    @ResponseBody
    @RequiresPermissions("jobsch:create")
    @PostMapping("/create")
    @SystemLog("注册接收验证码")
    public Result create(@Validated JobSchedule jobSchedule) {
        //log.info("访问到了controller");
        String username =(String)SecurityUtils.getSubject().getPrincipal();
        jobSchedule.setUsername(username);
        jobScheduleService.create(jobSchedule);


        webSocketService = SpringBeanUtil.getBean(WebSocketService.class);
        webSocketService.SendMessageToUser(username,"插入成功，订阅的消息");
        return Result.success();
    }

    @ResponseBody
    @RequiresPermissions("jobsch:update")
    @PostMapping("/update")
    public Result update(@Validated JobSchedule jobSchedule) {
        jobScheduleService.update(jobSchedule);
        return Result.success();
    }

    @ResponseBody
    @RequiresPermissions("jobsch:delete")
    @PostMapping("/delete")
    public Result delete(@RequestParam("id") Long[] ids) {
        jobScheduleService.delete(ids);
        return Result.success();
    }


    private void setCommonDate(Model model) {
        //log.info("controller jobNoList={}", JSON.toJSONString(jobNoService.list2Tree()));
        model.addAttribute("jobNoList", jobNoService.list2Tree());
        String username = (String) SecurityUtils.getSubject().getPrincipal();
        model.addAttribute("user", username);
    }


}
